
package com.adkfp.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.bean.ADKManageUserBean;
import com.adkfp.bean.ManageUserBean;
import com.adkfp.common.Constant;
import com.adkfp.service.IADKManageUserService;
import com.adkfp.service.ManageUserService;
import com.adkfp.utils.CommonUtil;
import com.adkfp.utils.MyUtils;
import com.adkfp.utils.SessionListener;

/*
 * 处理登陆界面
 */
@Controller
@RequestMapping(value="login")
public class LoginAction {
	static String username;
	
	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
	@Resource(name="aDKManageUserService")
	private IADKManageUserService aDKManageUserService;
	/**
	 * 登陆页面
	 */
	@RequestMapping(value="loginPage")
	public String loginPage(HttpServletRequest request, HttpServletResponse response, String name){
		return "login/login";
	}
	
	//登陆成功
	/**
	 * 登录的ajax
	 */
	/*@RequestMapping(value="tologin")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response, String userName, String password) {
		try {
			log.debug("系统登陆 参数信息 UserName: " + userName);

			ManageUserBean userBean = manageUserService.getUserByUserName(userName.trim(), 0);
			
			if (null != userBean) {
				
				if (CommonUtil.getMD5(password).equalsIgnoreCase(userBean.getPassword())) {
					
					// 设置Session
					HttpSession session = request.getSession();
					session.setAttribute(Constant.SESSION_USERID_LONG, userBean.getId());
					session.setAttribute(Constant.SESSION_USER_NAME_STRING, userBean.getUserName());
					session.setAttribute(Constant.SESSION_USER_BEAN, userBean);
					session.setAttribute("USER_ROLE", userBean.getRoleIdList().get(0));
					log.debug(userName+" 登陆成功");
					username = userName;
					return "OK";
					
				} else {
					log.debug(userName+" 密码错误");
					return "PASSWORD_ERROR";
				}
			} else {
				log.debug(userName+" 不存在");
				return "NO_EXIST";
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e, e);
			return "ERROR";
		} 
	}*/
	@RequestMapping(value="tologin")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response, String userName, String password,Integer role){
		if(userName!=null &&! userName.equals("") && password!=null && !password.equals("")) {
			ADKManageUserBean bean=aDKManageUserService.login(userName, password, role);
			if(bean!=null) {
				HttpSession session = request.getSession();
				session.setMaxInactiveInterval(7200);
				session.setAttribute(Constant.SESSION_USERID_LONG, bean.getId());
				session.setAttribute(Constant.SESSION_USER_NAME_STRING, bean.getUserName());
				session.setAttribute(Constant.SESSION_USERID_ROLE, role);
				session.setAttribute(Constant.SESSION_USER_BEAN, bean);
				session.setAttribute("USER_ROLE", bean.getRoleIdList().get(0));
				//System.out.println("zizizizizizi-------------"+bean);
				return "OK";
			}else {
				return "ERROR";
			}
			
		}else {
			return "ERROR";
		}
	}
	/**
	 * 退出
	 */
	@RequestMapping(value="loginout")
	public String loginout(HttpServletRequest request){
		
		// 设置Session
		HttpSession session = request.getSession();
		ManageUserBean userBean = (ManageUserBean) session.getAttribute(Constant.SESSION_USER_BEAN);
		if(username!=null) {
			SessionListener.sessionMap.remove( username );  
		}
		session.invalidate();
		return "login/login";
	}
}
